package com.brspace.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.entity.Process;
import com.brspace.qualitycontrol.entity.ProcessNode;
import com.brspace.qualitycontrol.entity.ProcessRouter;
import com.brspace.qualitycontrol.entity.Test;
import com.brspace.qualitycontrol.entity.vo.ProcessVo;
import com.brspace.qualitycontrol.mapper.*;
import com.brspace.qualitycontrol.service.TestService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hugo
 * @since 2021-03-25
 */
@Service
@SuppressWarnings({"all"})
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    @Resource
    private ProcessNodeMapper nodeMapper;
    @Resource
    private ProcessRouterMapper routerMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private ProcessMapper processMapper;

    /**
     * 保存请假单
     *@author Hugo
     *@date 2021-03-25 20:41
     **/
    @Override
    public void saveTest(Test test, int pId) {
        Map<String,Object> map =new HashMap<>(16);
        map.put("p_id",pId);
        map.put("sort_num",1);
        test.setNodeId(nodeMapper.selectOne(new QueryWrapper<ProcessNode>().allEq(map,false)).getId());
        baseMapper.insert(test);
    }


    /**
     * 提交审核
     *@author Hugo
     *@date 2021-03-31 10:42
     **/
    @Override
    public void examine(Test test) {
        if (test.getNodeId()==0){
            throw new ServiceException("流程已结束！");
        }
        // 获取当前流程下节点的最大排序值
        Integer maxNum = nodeMapper.sortMax(nodeMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id", test.getNodeId())).getPId());
        Map<String,Object> map = new HashMap<>(16);
        map.put("current_id",test.getNodeId());
        map.put("requirement",test.getProgress());
        int a = routerMapper.selectOne(new QueryWrapper<ProcessRouter>().allEq(map,false)).getTargetId();
        if (nodeMapper.selectById(a).getSortNum() == maxNum){
            test.setStatus(2);
            test.setNodeId(0);
        }else {
            test.setStatus(1);
            test.setNodeId(a);
        }
        baseMapper.updateById(test);
    }

    /**
     * 根据权限获取列表
     * @return
     */
    @Override
    public List<Test> findAll(String id) {
        // 流程ID
        int pid = processMapper.selectOne(new QueryWrapper<Process>().eq("flow_name", "请假流程")).getId();
        List<Test> tests = new ArrayList<>();
        ProcessVo list = nodeMapper.findById(id,pid);
        if (!EmptyUtil.isNotEmpty(list)) {
            throw new ServiceException("当前用户为普通");
        }
            System.err.println("用户查询数据：："+list);
            if (list.getRoleMa().equals("dept")){
                for (Test node : baseMapper.selectList(new QueryWrapper<Test>().eq("node_id", list.getId()).eq("tips",list.getDeptId()))) {
                    tests.add(node);
                }
                return tests;
            }
            for (Test node : baseMapper.selectList(new QueryWrapper<Test>().eq("node_id", list.getId()))) {
                tests.add(node);
            }
        return tests;
    }


}
